<?php
/**
*
* www.x23us.com
**/
namespace Common\Library;

class x23us{

    protected $BookArticle;
    protected $detailUrl;
    protected $bookInfo;
    protected $listUrl;
    protected $book_id;
    protected $article_id;

    protected $id;

    public function __construct($id,$url = ''){
        $this->id = $id;
        $this->detailUrl = $url;
        if(empty($this->detailUrl)){
            $this->update();
        }else{
            $this->addNewBook();
        }
        exit('over good');
    }

    //添加一本新书
    protected function addNewBook(){
        set_time_limit(0);
        $pathFile = './Runtime/Collect/x23us/addbook.html';
        \Common\Library\COM\CollectDo::downPage($this->detailUrl,$pathFile);
        $getStr = \Common\Library\COM\CollectDo::getHtml($pathFile);
        $this->bookInfo = \Common\Library\COM\CollectDo::getSubstr($getStr,'<dd><h1>','最新章节：');
        unset($getStr,$pathFile);
        //提取书籍名称
        $bookName = \Common\Library\COM\CollectDo::getSubstr($this->bookInfo,'',' 全文阅读');
        //提取作者
        $author = \Common\Library\COM\CollectDo::getSubstr($this->bookInfo,'文章作者</th><td>&nbsp;','</td><th>文章状态');
        //p($author);p($bookName);exit;
        if(empty($bookName) || empty($author)){
            $this->writeLog('书名,作者,或为空!:'.$this->detailUrl,'ERR');
        }
        //作者处理
        $author_id = D('Common/BookAuthor')->getAuthorId($author);
        $Book = new \Common\Model\BookModel();
        $unique = md5($bookName.$author_id);
        //书本重复检测
        $this->book_id = $Book->checkBookRepeart($unique);
        //p($book_id);exit;
        if(empty($this->book_id)){//新书入库

            //提取简介
            $intro = \Common\Library\COM\CollectDo::getSubstr($this->bookInfo,'<p>&nbsp;&nbsp;&nbsp;&nbsp;','</p><p style="display:none"');
            $intro = trim($intro);
            //提取分类
            $cate = \Common\Library\COM\CollectDo::getSubstr($this->bookInfo,'.html">','</a></td><th>');
            if(empty($cate)){
                $this->writeLog('分类为空:'.var_export($this->bookInfo,1),'ERR');
            }

            //书籍状态
            $bookStatus = \Common\Library\COM\CollectDo::getSubstr($this->bookInfo,'文章状态</th><td>&nbsp;','</td></tr>');
            if($bookStatus == '连载中'){
                $status = 'ing';
            }else{
                $status = 'end';
            }
            //分类处理
            $cate_id = D('Common/BookCate')->getCateId($cate);
            //封面图片处理
            $imagePath = \Common\Library\COM\CollectDo::doImagePath();

            $dataBook = array(
                'book_name'=>$bookName,
                'author_id'=>$author_id,
                'cate_id'=>$cate_id,
                'introduction'=>  htmlentities($intro,ENT_COMPAT,'UTF-8'),
                'img'=>$imagePath,
                'create_time'=>  time(),
                'year'=> date('Y'),
                'month'=>  date('m'),
                'day'=>  date('d'),
                'unique'=>$unique
            );
            $this->book_id = $Book->saveBook($dataBook);
        }
        unset($bookInfo,$start,$end,$Book);
        //新书开始入库
        if($this->book_id > 0){
            $this->writeLog('bookinstall.新书开始入库:'.var_export($bookName,1));

            //提取封面图片
            $image = \Common\Library\COM\CollectDo::getSubstr($this->bookInfo,'src="','"  onerror=');
            \Common\Library\COM\CollectDo::downPage('http://www.x23us.com'.$image, '.'.$imagePath);
            $this->listUrl = \Common\Library\COM\CollectDo::getSubstr($this->bookInfo,'<a class="read" href="','" title="');
            $this->articleCollection();
        }else{
            $this->writeLog('入库错误,书本未入库:'.var_export($dataBook,1),'ERR');
        }

    }

    //章节列表名称入库
    protected function articleCollection(){
        if(empty($this->listUrl) || empty($this->book_id)){
            $this->writeLog('列表地址和主键id为空'.var_export($this->bookInfo,1),'ERR');
        }
        $pathFile = './Runtime/Collect/x23us/listbook.html';
        \Common\Library\COM\CollectDo::downPage($this->listUrl,$pathFile);
        $getStr = \Common\Library\COM\CollectDo::getHtml($pathFile);
        $this->listInfo = \Common\Library\COM\CollectDo::getSubstr($getStr,'bgcolor="#E4E4E4" id="at">','</table></dd>');

        $count = mb_substr_count($this->listInfo,'<a href="');
        $this->BookArticle = new \Common\Model\BookArticleModel();
        //获取最新章节
        //$new = $this->BookArticle->where("`book_id`='{$this->book_id}'")->count('article_id');
        // if($new['article_num'] > 0 && $new['article_num'] < $count){
        //     $continuef = true;
        // }else{
        //     $continuef = FALSE;
        // }

        for($i=0;$i<$count;$i++){
            $now =  \Common\Library\COM\CollectDo::getSubstr($this->listInfo,'class="L">','</a></td>',FALSE);
            $article_name = \Common\Library\COM\CollectDo::getSubstr($now,'html">');
            if(empty($article_name)){
                $this->writeLog('章节名称不能为空!:'.$now.'@URL:'.$now,'ERR');
            }
            $article_num = \Common\Library\COM\CollectDo::getArticleNum($article_name);
            $unique = md5($this->book_id.$article_name);
            $data = array('book_id'=>$this->book_id,'article_num'=>$article_num,'article_name'=>$article_name);
            $result = $this->BookArticle->checkArtcileRepeat($unique,$data);
            if($result){//重复章节检测
                $this->writeLog($article_name.' ====> 重复章节');
            }else{
                $this->writeLog($article_name.' ====> 章节入库');

                $articleDate = array(
                    'book_id'=> $this->book_id,
                    'article_name'=> $article_name,
                    'create_time'=>  time(),
                    'year'=>  date('Y'),
                    'month'=>  date('m'),
                    'day'=>  date('d'),
                    'article_num'=> $article_num,
                    'update_time' => time(),
                    'unique'=>$unique,
                    //'piece_num'=> $piece_num,
                    //'piece_name'=> $piece_name,
                    'content'=>$contentPath
                );
                //p($articleDate);exit;
                $this->article_id = $this->BookArticle->add($articleDate);
                if(empty($this->article_id)){
                    $this->writeLog('章节存储失败!@'.var_export($articleDate,1).var_export($this->BookArticle->getDbError(),1),'ERR');
                }else{
                    //获取章节内容url地址
                    $article_url = \Common\Library\COM\CollectDo::getSubstr($now,'<a href="','">',FALSE);
                    //获取章节内容并存储
                    $contentPath = $this->contentCollection($this->listUrl.$article_url);
                }
            }
            $this->listInfo = \Common\Library\COM\CollectDo::getSubstr($this->listInfo,'</a></td>','',FALSE);
        }
        unset($this->BookArticle);
        $this->writeLog($this->detailUrl.'  本书入库完成!');
    }

    //章节内容入库
    protected function contentCollection($contentUrl = ''){
        if(empty($contentUrl)){
            return FALSE;
        }
        $savePath = './Runtime/Collect/x23us/content.html';
        \Common\Library\COM\CollectDo::downPage($contentUrl, $savePath);
        $contentA = \Common\Library\COM\CollectDo::getHtml($savePath);
        if(empty($contentA)){
            \Common\Library\COM\CollectDo::downPage($contentUrl, $savePath);
            sleep(2);
            $contentA = \Common\Library\COM\CollectDo::getHtml($savePath);
        }
        $contentA = \Common\Library\COM\CollectDo::getSubstr($contentA,'<dd id="contents">','<div class="adhtml"><script>show_htm2()');
        $replace = array('<a href=','<a','</a>','x23us','script');
        $contentT = str_replace($replace,'', $contentA);
        if(!\Common\Library\COM\CollectDo::checkSpecialStr($contentT)){
            $data = array('id'=>$this->id,'disabled'=>4,'remark'=>'采集章节内容有误,包含特殊字符:'.$contentUrl);
            \HelloAdmin\Model\CrontabModel::updateData($data);
            $this->writeLog('采集章节内容有误,包含特殊字符:'.$contentUrl.'@@'.var_export($contentT,1),'ERR');
        }
        $contentT = htmlentities($contentT,ENT_COMPAT,'UTF-8');
        $contentPath = \Common\Library\COM\CollectDo::doContentPath();
        $handel = fopen('.'.$contentPath,'x+');
        if($handel && fwrite($handel,$contentT) !== FALSE){
            fclose($handel);
            return $contentPath;
        }
        $this->writeLog('章节内容存储失败!@'.var_export($contentPath,1).'|@|'.var_export($contentUrl,1),'ERR');
    }

    //采集日志记录
    private function writeLog($message,$level='INFO'){
        \Think\Log::write($message,$level,$type='','./Runtime/Collect/x23us/logs-'.  date('Ymd').'.log');
        if($level == 'ERR'){
            exit($message);
        }
    }

    public function update(){
        $url = 'http://www.x23us.com/top/lastupdate_1.html';
        set_time_limit(0);
        $pathFile = './Runtime/Collect/x23us/updatelist.html';
        \Common\Library\COM\CollectDo::downPage($url, $pathFile);
        $getStr = \Common\Library\COM\CollectDo::getHtml($pathFile);
        $updatelist = \Common\Library\COM\CollectDo::getSubstr($getStr,'状态</th>','</table>');
        unset($getStr,$pathFile);
        //获取书籍名称
        $count = mb_substr_count($updatelist,'<tr bgcolor="#FFFFFF">');
        $Book = new \Common\Model\BookModel();
        $Author = new \Common\Model\BookAuthorModel();
        for($i=0;$i<$count;$i++){
            $now = \Common\Library\COM\CollectDo::getSubstr($updatelist,'<td class="L">','</tr>',FALSE);
            $bookName = \Common\Library\COM\CollectDo::getSubstr($now,'target="_blank">','</a></td>');
            $authorName = \Common\Library\COM\CollectDo::getSubstr($now,'<td class="C">','<td class="R">');
            $authorName = trim(strip_tags($authorName));
            if(empty($bookName) || empty($authorName)){
                $this->writeLog('书名,作者,或为空!:'.var_export($now,1).'@@'.var_export($updatelist,1),'ERR');
            }
            $author_id = $Author->getAuthorId($authorName);
            $unique = md5($bookName.$author_id);
            //书本重复检测
            $this->book_id = $Book->checkBookRepeart($unique);
            if($this->book_id > 0){//更新已有的书籍
                $articleName = \Common\Library\COM\CollectDo::getSubstr($now,'target="_blank"> ','<td class="C"');
                $articleName = trim(strip_tags($articleName));
                if(empty($articleName)){
                    $this->writeLog($offset.':====> 章节名称空'.var_export($now,1),'ERR');
                }
                $unique = md5($this->book_id.$articleName);
                $this->BookArticle = new \Common\Model\BookArticleModel();
                $result = $this->BookArticle->checkArtcileRepeat($unique);

                if($result){//重复章节检测
                    $this->writeLog($articleName.' ====> 重复章节');
                    continue;
                }else{
                    //获取章节数
                    $num = \Common\Library\COM\CollectDo::getArticleNum($articleName);
                    $piece_num = \Common\Library\COM\CollectDo::getPieceNum($articleName);
                    //获取章节内容地址
                    $articleUrl = \Common\Library\COM\CollectDo::getSubstr($now,'</a></td>','<td class="C">');
                    $articleUrl = \Common\Library\COM\CollectDo::getSubstr($articleUrl,'<a href="','" target="_blank">');
                    //p($value);p($articleUrl);exit;
                    $new = $this->BookArticle->getNewArticle($this->book_id);
                    if(empty($num) || empty($new['article_num']) || $new['article_num']==($num-1)){//默认更新一个章节
                        //获取章节内容并存储
                        $contentPath = $this->contentCollection($articleUrl);
                        $articleDate = array(
                            'book_id'=> $this->book_id,
                            'article_name'=> $articleName,
                            'create_time'=>  time(),
                            'year'=>  date('Y'),
                            'month'=>  date('m'),
                            'day'=>  date('d'),
                            'article_num'=> $num,
                            'update_time' => time(),
                            'unique'=>$unique,
                            'piece_num'=> $piece_num,
                            'piece_name'=> '',
                            'content'=>$contentPath
                        );
                        //p($articleDate);//exit;
                        $this->article_id = $this->BookArticle->add($articleDate);
                        $this->writeLog('====> 更新了 一个 章节 ======>');
                        continue;
                    }else{//更新多个章节
                        $this->listUrl = \Common\Library\COM\CollectDo::getSubstr($now,'</a><a href="','" target="_blank">');
                        //章节名称和篇名入库
                        $this->articleCollection();
                    }
                }
            }else{
                 $this->writeLog($bookName.'||====> 新书跳过，更新下一个章节 ====>');
            }
            $updatelist = \Common\Library\COM\CollectDo::getSubstr($updatelist,'</tr>','',FALSE);
            continue;
        }
    }

}
